package com.ct.gd.passport.view.passport;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.TextView;

import com.ct.gd.passport.R;
import com.ct.gd.passport.common.BaseActivity;
import com.ct.gd.passport.common.constants.UmengVar;
import com.ct.gd.passport.domain.Helper;
import com.ct.gd.passport.view.UserProtocolActivity;
import com.ct.gd.passport.view.widget.CustomHeaderFragment;
import com.umeng.analytics.MobclickAgent;
import com.up91.common.android.view.adapter.SimpleListAdapter;

public class PassportHelperActivity extends BaseActivity implements OnClickListener, OnItemClickListener {

	private CustomHeaderFragment	mFgHeader;

	// Button mSubmitButton;
	// Button mHistoryButton;
	// Button mSubmitWeekendButton;
	private GridView				mAreaEntries;

	private static class Entry {
		int							title;
		int							icon;
		Class<? extends Activity>	targetClass;
		String eventId;

		public Entry(int title, int icon, Class<? extends Activity> targetClass,String eventId) {
			super();
			this.title = title;
			this.icon = icon;
			this.targetClass = targetClass;
			this.eventId = eventId;
		}
	}

	private static Entry[]	sEntries	= { 
			new Entry(R.string.passport_submit, R.drawable.ic_passport_submit, PassportAppGuideActivity.class,UmengVar.PASSPORT_SQ), 
			new Entry(R.string.passport_history, R.drawable.ic_passport_query, PassportAppHistoryActivity.class,UmengVar.PASSPORT_MY), 
			new Entry(R.string.passport_query_use_number, R.drawable.ic_passport_history, PassportQueryActivity.class,UmengVar.PASSPORT_CODE_QUERY), 
			new Entry(R.string.passport_submit_weekend, R.drawable.ic_passport_submit_weekend, PassportWeekendReservationActivity.class,UmengVar.PASSPORT_IMMI), 
			new Entry(R.string.passport_submit_weekend_query, R.drawable.ic_passport_submit_weekend_query, PassportWeekendReservationQueryActivity.class,UmengVar.PASSPORT_IMMI_QUERY), 
	};

	@Override
	protected void onBaseCreate(Bundle savedInstanceState) {
		setContentView(R.layout.passport_helper);
	}

	@Override
	protected void initViews() {
		mFgHeader = (CustomHeaderFragment) getSupportFragmentManager().findFragmentById(R.id.header);
		mFgHeader.setCenterText(R.string.passport_helper);

		mAreaEntries = (GridView) findViewById(R.id.area_passport_helper_entries);
		mAreaEntries.setOnItemClickListener(this);

		mAreaEntries.setAdapter(new EntriesAdapter(this, sEntries));
		// mSubmitButton =
		// (Button)findViewById(R.id.btn_passport_helper_submit);
		// mHistoryButton =
		// (Button)findViewById(R.id.btn_passport_helper_history);
		// mSubmitWeekendButton =
		// (Button)findViewById(R.id.btn_passport_helper_weekend);
		//
		// mSubmitButton.setOnClickListener(this);
		// mHistoryButton.setOnClickListener(this);
		// mSubmitWeekendButton.setOnClickListener(this);
	}
	
	@Override
	protected void onAfterCreate(Bundle savedInstanceStat) {
		if(Helper.hasAgreeUserProtocol(this) == false){
			startActivityForResult(new Intent(this,UserProtocolActivity.class), 0);
		}
		super.onAfterCreate(savedInstanceStat);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Class<? extends Activity> target = (Class<? extends Activity>) view.getTag();
		MobclickAgent.onEvent(this, sEntries[position].eventId);
		if (null != target) startActivity(new Intent(this, target));
	}

	private class EntriesAdapter extends SimpleListAdapter<Entry> {
		public EntriesAdapter(Context context, Entry[] data) {
			super(context, data);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			TextView btn;
			if (null == convertView) {
				btn = (TextView) getLayoutInflater().inflate(R.layout.passport_helper_entries_item, parent, false);
			} else btn = (TextView) convertView;

			Entry entry = getData().get(position);
			btn.setText(entry.title);
			btn.setCompoundDrawablesWithIntrinsicBounds(0, entry.icon, 0, 0);
			btn.setTag(entry.targetClass);
			return btn;
		}

	}

	@Override
	public void onClick(View v) {
		// switch (v.getId()) {
		// case R.id.btn_passport_helper_submit:
		// startActivity(new Intent(this, PassportAppGuideActivity.class));
		// break;
		// case R.id.btn_passport_helper_history:
		// startActivity(new Intent(this, PassportAppHistoryActivity.class));
		// break;
		// case R.id.btn_passport_helper_weekend:
		// startActivity(new Intent(this,
		// PassportWeekendReservationActivity.class));
		// break;
		// default:
		// break;
		// }
	}

}
